const express = require('express');
      bodyParser = require('body-parser')
      router = express.Router();
      cookieParser = require('cookie-parser')

app.use(bodyParser.urlencoded({extended:true}))
app.use(cookieParser())
//提问部分
app.post('/ask',(req,res) => {
    var time = new Date()
    var name = req.cookies.username
    req.body.name = name
    req.body.time = time
    console.log(name)
    var ask = new Ask(req.body)
    ask.save(function(err,data){
      
      var askId = data.id
      // console.log(askId)
      res.cookie('askId',askId)
      send(res,'success','登陆成功..')
    })
    
})

router.get('/thid/:thisId',(req,res)=>{
	var id = req.params.thisId
	console.log(id)
	Ask.find({_id:id}).populate('userId').exec(function(err,data){
		console.log(data[0].userId.id)
		res.send({init:data[0].userId.id})
	})
	
	
})

router.post('/del',(req,res)=>{
  var id=req.body.thisId
  var userId=req.body.userId
  console.log(id)
  console.log(userId)
  Answer.remove({askId:id},function(err,data){
				     console.log(data)
				  })
 Ask.find({_id:id},function(err,result){
    var resId=result[0].userId
    console.log(resId)
		if(resId==userId){
					setTimeout(function(){
			      	Ask.remove({_id:id},function(err,data){
					     if(err){
					        send(res,'fail','删除失败')
					     }else{
					        send(res,'success','删除成功')
					     }
				    })
		      },100)
	  }
	})
  
  
})
//提问页面渲染
router.get('/ask',(req,res) =>{
  res.render('ask',{
    title:'提问'
  })
})

module.exports = router